<!DOCTYPE html>
<html>

<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        body {
            margin: 0 auto;
            padding: 0px;
            font-size: 12px;
            background: url(images/bg.gif) repeat center 36px;
            text-align: center;
            background-color: #c30230;
        }
        
        #content {
            margin: 0 auto;
            width: 960px;
            background: url(images/content_bg.jpg) no-repeat left top;
            height: 627px;
        }
        
        #content .tip1,
        #content .tip2,
        #content .tip3,
        #content .tip4,
        #content .tip5,
        #content .tip6,
        #content .tip7,
        #content .tip8 {
            position: absolute;
            width: 227px;
            left: 200px;
            top: 100px;
        }
        
        #content .tip1 .tip_h {
            background: url(images/tip1_h.gif) no-repeat left top;
        }
        
        #content .tip1 .tip_h,
        #content .tip2 .tip_h,
        #content .tip3 .tip_h,
        #content .tip4 .tip_h,
        #content .tip5 .tip_h,
        #content .tip6 .tip_h,
        #content .tip7 .tip_h,
        #content .tip8 .tip_h {
            width: 227px;
            padding-top: 45px;
            height: 23px;
            text-align: left;
            cursor: move;
        }
        
        #content .tip1 .tip_c {
            background: url(images/tip1_c.gif) repeat-y;
        }
        
        #content .tip1 .tip_c,
        #content .tip2 .tip_c,
        #content .tip3 .tip_c,
        #content .tip4 .tip_c,
        #content .tip5 .tip_c,
        #content .tip6 .tip_c,
        #content .tip7 .tip_c,
        #content .tip8 .tip_c {
            width: 200px;
            padding-left: 12px;
            padding-right: 15px;
            min-height: 40px;
            text-align: left;
            line-height: 20px;
            max-height: 160px;
            word-wrap: break-word;
            word-break: break-all;
            overflow: hidden;
        }
        
        #content .tip1 .tip_f {
            background: url(images/tip1_f.gif) no-repeat left top;
        }
        
        #content .tip1 .tip_f,
        #content .tip2 .tip_f,
        #content .tip3 .tip_f,
        #content .tip4 .tip_f,
        #content .tip5 .tip_f,
        #content .tip6 .tip_f,
        #content .tip7 .tip_f,
        #content .tip8 .tip_f {
            width: 227px;
            height: 53px;
            padding-top: 20px;
        }
        
        #content .close,
        #content .close2 {
            float: left;
            font-size: 12px;
            cursor: pointer;
            color: #000000;
        }
        
        .clr {
            clear: both;
            overflow: auto;
            display: block;
            height: 0px;
        }
        
        #content .icon {
            float: left;
            width: 35px;
            padding-left: 15px;
            height: 35px;
            text-align: center;
        }
        
        #content .name {
            float: right;
            padding-right: 15px;
            text-align: right;
            font-size: 14px;
            line-height: 35px;
            color: #C0F;
        }
        
        #content .num {
            float: left;
            padding-left: 7px;
            width: 195px;
        }
        
        #count {
            font-size: 20px;
            font-weight: 600;
            line-height: 28px;
            color: #222;
        }
        
        #comment {
            padding: 20px;
            box-sizing: border-box;
            height: 180px;
            margin-top: 20px;
            border-radius: 20px;
            position: relative;
            overflow: hidden;
            border-radius: 8px;
            padding: 16px;
            background: #f8f8f8;
        }
        
        #comment textarea {
            display: block;
            border: 0;
            padding: 0;
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
            max-height: 72px;
            width: 100%;
            height: 150px;
            font-size: 16px;
            line-height: 24px;
            color: #222;
            outline: none;
            resize: none;
            background: #f8f8f8;
            -webkit-transition: height .3s ease-in-out;
            -moz-transition: height .3s ease-in-out;
            transition: height .3s ease-in-out;
        }
        
        #comment button {
            border: none;
            border-radius: 4px;
            padding: 6px 18px;
            font-size: 14px;
            font-weight: 500;
            line-height: 20px;
            color: #fff;
            background: hotpink;
            cursor: pointer;
            outline: none;
        }
        
        #comment button:hover {
            background: deeppink;
        }
        
        #comment-box {
            width: 960px;
            margin: 0 auto;
            background: #fff;
        }
        
        .action {
            text-align: right;
        }
        
        #count {
            text-align: left;
            margin-bottom: 10px;
        }
    </style>
</head>

<body>
    <div id="content">
        <!-- <div class="tip1" id="cc">
            <div class="tip_h" title="双击关闭纸条">
                <div class="num">第[49]条 2022-02-17 22:51:52</div>
                <div class="close" title="关闭纸条">×</div>
                <div class="clr"></div>
            </div>
            <div class="tip_c">
                希望马上赚2个亿
            </div>
            <div class="tip_f">
                <div class="icon">
                    <img src="images/bpic_1.gif" alt="" title="">
                </div>
                <div class="name">成真的人</div>
                <div class="clr"></div>
            </div>
        </div> -->
    </div>
    <div id="comment-box">
        <div id="comment">
            <div id="count">
                <b id="count--b">评论:0</b>
            </div>
            <div id="ipt">
                <textarea placeholder="说点什么吧..." resize="none" name="" id="ipt--textarea" cols="30" rows="10"></textarea>
                <div class="action">
                    <button id="submit">发送评论</button>
                </div>
            </div>

        </div>
    </div>
</body>

</html>

<script crossorigin="anonymous" integrity="sha512-n/4gHW3atM3QqRcbCn6ewmpxcLAHGaDjpEBu4xZd47N0W2oQ+6q7oc3PXstrJYXcbNU1OHdQ1T7pAP+gi5Yu8g==" src="//lib.baomitu.com/jquery/3.6.0/jquery.js"></script>
<script>
    //获取DOM元素
    let content = document.getElementById("content");
    let count = document.getElementById("count--b");
    let ipt = document.getElementById("ipt--textarea");
    let submit = document.getElementById("submit");

    //设置层级
    let z_index = 1;

    //初次渲染页面
    render();

    //渲染评论区条目
    function render() {
        $.ajax({
            url: "http://chst.vip:1902/students/getstulist",
            type: "get",

            //成功执行success
            success: function(res) {
                console.log(res);
                let data = res.data;
                //修改评论条数
                count.innerHTML = `评论:${data.length}条`;

                //渲染 小纸条结构
                //根据data.length 动态渲染
                var html = "";
                for (var i = 0; i < data.length; i++) {
                    var msg = data[i];
                    //console.log(msg);
                    var tips = document.createElement("div");
                    //通过id进行绑定
                    tips.id = msg.id;
                    tips.className = "tip1";
                    html = `
                    <div class="tip_h" title="双击关闭纸条">
                <div class="num">第[${msg.id.slice(8)}]条 ${msg.time}</div>
                <div class="close" title="关闭纸条">×</div>
                <div class="clr"></div>
            </div>
            <div class="tip_c">
                ${msg.content}
            </div>
            <div class="tip_f">
                <div class="icon">
                    <img src="images/bpic_1.gif" alt="" title="">
                </div>
                <div class="name">${msg.name}</div>
                <div class="clr"></div>
            </div>
                    `;
                    tips.innerHTML = html;
                    content.appendChild(tips);

                    //进行随机生成
                    //console.log(content.offsetLeft);
                    let x = Math.random() * 400 + 544;
                    let y = Math.random() * 400;

                    tips.style.left = x + "px";
                    tips.style.top = y + "px";

                    //删除小纸条
                    let close = tips.getElementsByClassName("close")[0];
                    close.onclick = function() {
                        let id = this.parentNode.parentNode.id;
                        let tip = this.parentNode.parentNode;
                        del(tips, id);
                    }

                    //双击头部 删除小纸条
                    let header = tips.firstElementChild;
                    //console.log(header);
                    header.ondblclick = function() {
                        console.log(header);
                        //找到当前双击的元素
                        var tip = this.parentNode;
                        let id = this.parentNode.id;
                        del(tip, id);
                    }

                    //点击小纸条 改变层级

                    tips.onclick = function() {
                        z_index++;
                        this.style.zIndex = z_index;
                    }


                    //拖拽小纸条 进行 移动
                    header.onmousedown = function(e) {
                        //console.log(123);
                        let x = e.offsetX;
                        let y = e.offsetY;

                        //保存this
                        let _this = this;

                        content.onmousemove = function(e) {
                            //移动位置
                            let moveX = e.pageX - x;
                            let moveY = e.pageY - y;
                            //水平位置
                            let maxL = content.offsetWidth + content.offsetLeft - _this.offsetWidth;
                            let minL = content.offsetLeft;

                            //垂直位置
                            let maxT = content.offsetHeight - _this.parentNode.offsetHeight; //最大的垂直位置
                            let minT = 0;

                            if (moveX < minL) {
                                moveX = minL;
                            } else if (moveX > maxL) {
                                moveX = maxL;
                            }

                            if (moveY < minT) {
                                moveY = minT;
                            } else if (moveY > maxT) {
                                moveY = maxT;
                            }

                            _this.parentNode.style.left = moveX + "px";
                            _this.parentNode.style.top = moveY + "px";
                        }

                    }

                    //鼠标弹起事件
                    header.onmouseup = function() {
                            content.onmousemove = null;
                        }
                        // content.onmouseleave = function() { //注册这个事件放置鼠标拖出盒子外后再回来,纸条还跟随移动的问题
                        //     this.onmousemove = null;
                        // }
                }
            }
        })
    }


    //删除小纸条
    function del(el, id) {
        let flag = confirm("你确定要删除吗？");
        if (!flag) {
            alert("你已经取消删除");
            return;
        } else {
            $.ajax({
                    url: "http://chst.vip:1902/students/del",
                    type: "get",
                    data: {
                        id
                    }
                })
                .then(res => {
                    console.log(res);
                    if (res.state) {
                        content.removeChild(el);
                        alert("删除成功");
                        render();
                    } else {
                        alert("删除失败");
                    }
                })
        }

    }



    function btnPrevent() {
        //禁用提交按钮
        let val = ipt.value;
        //进行验证，非空则启用，为空则禁用
        if (!val) {
            submit.disabled = true;
            submit.style.background = "gray";
        } else {
            submit.disabled = false;
            submit.style.background = "pink";
        }
    }
    btnPrevent();

    //评论区输入
    ipt.oninput = function() {
        //console.log(999);
        // submit.disabled = false;
        // submit.style.background = "pink";
        btnPrevent();
    }

    //提交信息
    function submitData() {
        console.log(666);
        let val = ipt.value;
        let name = prompt("请输入你的昵称");
        if (!name) {
            alert("昵称不能为空");
            return;
        }
        $.ajax({
                url: "http://chst.vip:1902/students/addlist",
                type: "post",
                data: {
                    name: name,
                    content: val
                }
            })
            .then(res => {
                if (res.state) {
                    alert("添加成功");
                    render();
                } else {
                    alert("添加失败");
                }
            })
    }

    //点击按钮
    submit.onclick = function() {
            submitData();
        }
        // //回车添加数据
    document.onkeydown = function(e) {
        if (e.keyCode === 13 && !submit.disabled) {
            submitData();
        }
    }
</script>